//
//  KKMessageItem.m
//  ReplyAssistant
//
//  Created by liaozhenming on 2018/11/22.
//  Copyright © 2018 KK. All rights reserved.
//

#import "KKMessageItem.h"
#import <objc/runtime.h>

#import "KKChatMsgTableViewCells.h"

@implementation KKMessageItem

- (instancetype)initWithMsg:(RAMessage*)msg type:(KKMessageType)type {
    self = [super init];
    if (self) {
        self.msg = msg;
        self.type = type;
    }
    return self;
}


+ (instancetype)msgWithDate:(NSDate*)timetips{
    RAMessage *msg = [[RAMessage alloc] init];
    msg.timestamp = [timetips timeIntervalSince1970] * 1000;
    return [[KKMessageItem alloc] initWithMsg: msg type: KKMessageType_TimeTip];
}

+ (instancetype)msgWithTMessage:(RAMessage*)msg {
    return [[KKMessageItem alloc] initWithMsg: msg type: (NSInteger)msg.type];
}

- (BOOL)isMineMsg {
    return self.msg.isSelf;
}

@end

#pragma mark -
#pragma mark - KKMessageItem(UITableViewCell) Class

static CGFloat KKMessageItem_Content_DefaultHeight = 30.0;

@implementation KKMessageItem(UITableViewCell)

@dynamic cellHeight;
static const void *KKMessageItem_CellHeightValue = &KKMessageItem_CellHeightValue;
-(void)setCellHeight:(CGFloat)cellHeight {
    objc_setAssociatedObject(self, KKMessageItem_CellHeightValue, @(cellHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)cellHeight {
    return [objc_getAssociatedObject(self, KKMessageItem_CellHeightValue) floatValue];
}

@dynamic cellContentSize;
static const void *KKMessageItem_CellContentSizeValue = &KKMessageItem_CellContentSizeValue;
- (void)setCellContentSize:(CGSize)cellContentSize {
    objc_setAssociatedObject(self, KKMessageItem_CellContentSizeValue, [NSValue valueWithCGSize: cellContentSize], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGSize)cellContentSize {
    return [objc_getAssociatedObject(self, KKMessageItem_CellContentSizeValue) CGSizeValue];
}

#pragma mark - Public methods
//  获取cell
- (KKChatMsgBaseTableViewCell*)tableView:(UITableView *)tableView style:(KKChatMsgBaseTableViewCellStyle)style {
    NSString *reuseId = [self msgCellReuseIndentifier];
    KKChatMsgBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: reuseId];
    if (cell == nil) {
        if (style == KKChatMsgBaseTableViewCellStyle_C2C) {
            cell = [[[self showCellClass] alloc] initWithC2CReuseIdentifier: reuseId];
        }
        else {
            cell = [[[self showCellClass] alloc] initWithGroupReuseIdentifier: reuseId];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

//  获取cell的高度
- (CGFloat)heightInWidth:(CGFloat)width style:(KKChatMsgBaseTableViewCellStyle)style {
    //  如果cell的Height不为0，则显示height值，否则计算height的值，并且存储起来
    if (self.cellHeight != 0.0) {
        return self.cellHeight;
    }
    
    if (self.type == KKMessageType_TimeTip) {
        return 20.0;
    }
    else {
        
        //  内容区域
        CGSize contentSize = [self contentSizeInWidth: width];
        
        CGFloat spaceHeight = 40.0;
        if (style == KKChatMsgBaseTableViewCellStyle_Group) {
            //  显示群成员昵称，及其其他的信息
            spaceHeight += 40;
        }
        self.cellHeight = contentSize.height + spaceHeight;
        return self.cellHeight;
    }
}

- (CGSize)contentSizeInWidth:(CGFloat)width {
    if (self.cellContentSize.width != 0) {
        return self.cellContentSize;
    }
    
    if (![self.msg isKindOfClass: [RAMessage class]]) {
        return CGSizeZero;
    }
    
    NSInteger maxWidth = SCREEN_WIDTH * 0.6;
    if (width > maxWidth) {
        width = maxWidth;
    }
    
    CGSize contentSize = [self.msg kk_contentSizeWithWidth: width];
    
//    self.cellContentSize = CGSizeMake(contentSize.width, contentSize.height < KKMessageItem_Content_DefaultHeight ? KKMessageItem_Content_DefaultHeight : contentSize.height);
    self.cellContentSize = contentSize;
    return self.cellContentSize;
}

#pragma mark - Private methods
- (NSString*)msgCellReuseIndentifier {
    return NSStringFromClass([self showCellClass]);
}

- (Class)showCellClass {
    switch (self.type) {
        case KKMessageType_Text:
            return [KKChatTextMessageCell class];
            break;
        case KKMessageType_TimeTip:
            return [KKChatTimeTipsMessageCell class];
            break;
        case KKMessageType_Image:
            return [KKChatImageMessageCell class];
            break;
        default:
            return [KKChatMsgBaseTableViewCell class];
            break;
    }
}

@end
